package org.example.service;

import cn.hutool.json.JSONUtil;
import com.google.protobuf.InvalidProtocolBufferException;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.example.test.helloworld.OperateGrpc;
import org.example.test.helloworld.OperateType;
import org.example.test.helloworld.Request;
import org.springframework.stereotype.Service;

/**
 * @author liyishan
 * @date 2023/7/21 14:03
 * @apiNote
 */
@Service
public class OperateClient {
    @GrpcClient("operateImpl")
    private OperateGrpc.OperateBlockingStub operateBlockingStub;

    public double receiveGreeting(double a, double b) {
        Request request = Request.newBuilder()
                .setNum1(a).setNum2(b).setOpType(OperateType.Addition)
                .build();
        return operateBlockingStub.calculate(request).getResult();
    }

    public static void main(String[] args) throws InvalidProtocolBufferException {
        Request request = Request.newBuilder()
            .setNum1(1).setNum2(2).setOpType(OperateType.Addition)
            .build();
        byte[] serialized = request.toByteArray();
        // 反序列化
        Request p2 = Request.parseFrom(serialized);
        System.out.println(p2);
    }
}
